package com.example.demo.redis.repository;

import com.example.demo.model.Material;
import com.example.demo.redis.mapper.RedisTestMapper;
import com.example.demo.redis.mapper.RedisZhuJieMapper;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by 86151 on 2018/12/28.
 */
@Component
public class RedisZhuJieRepository {
    @Autowired
    private RedisZhuJieMapper redisZhuJieMapper;
    @Autowired
    private RedisTestMapper redisTestMapper;

    //查询后新增数据到redis数据库中
    @Cacheable(value = "testRedis",key = "'queryAll'")
    public Result<List<Material>> queryAll() {
        System.out.println("我通过数据库获取到了数据！");
        List<Material> materials = redisZhuJieMapper.queryAll();
        return ResultVo.success(materials);
    }

    @Cacheable(value = "testRedis",key = "'queryMaterialById'+#id")
    public Result<Material> queryMaterialById(Integer id) {
        System.out.println("我通过数据库获取到了数据！");
        Material materials = redisTestMapper.queryMarterialById(id);
        return ResultVo.success(materials);
    }



    //修改数据到redis数据库中
    @CachePut(value = "testRedis",key = "'queryMaterialById'+#material.id")
    @CacheEvict(value = "testRedis",key = "'queryAll'")
    public Result<Material> updateRedis(Material material) {
        redisTestMapper.updateMaterial(material);
        System.out.println("我通过数据库获取到了数据！");
        Material materials = redisTestMapper.queryMarterialById(material.getId());
        return ResultVo.success(materials);
    }


    //删除redis数据库中数据
    @CacheEvict(value = "testRedis",key = "'queryMaterialById'+#id" )
    public int delete(Integer id) {
        delete();
        System.out.println("通过数据库获操作数据！");
        return redisTestMapper.deleteMaterial(id);
    }

    @CacheEvict(value = "testRedis",key = "'queryAll'")
    public void delete(){

    }
}
